package com.charging.business.media;

import com.charging.business.utils.ToolsUtil;

/**
 * CPC卡基本信息文件(MF/EF02)的编码信息
 * @author lzj
 */
public class CpcMfEf02EdModel {
    public static final int MSG_LEN = 2;

    /**
     * 电量信息(1,1,n)
     * 最高位： 0 -正常 1- 低电;
     * 其他位：剩余电量百分比
     */
    public byte[] electricityInfo = new byte[1];

    /**
     * 5.8GHz工作状态(2,1,n)
     * 0-关闭; 1-打开
     */
    public byte[] workState = new byte[1];

    /**
     * BIN转实体
     *
     * @param buff
     * @return
     */
    public static CpcMfEf02EdModel parse(byte[] buff) {
        int index = 0;
        CpcMfEf02EdModel ret = new CpcMfEf02EdModel();

        index += ToolsUtil.arrcopy(buff, index, ret.electricityInfo);
        index += ToolsUtil.arrcopy(buff, index, ret.workState);
        return ret;
    }

    /**
     * HEX字符串转实体
     *
     * @param strHex
     * @return
     */
    public static CpcMfEf02EdModel parse(String strHex) {
        if (strHex == null || strHex.isEmpty()) {
            return null;
        }
//        if (strHex.length() < CPCMFEF02ED.MSG_LEN * 2) {
//            return null;
//        }
        if (!ToolsUtil.chkHexChar(strHex)) {
            return null;
        }
        byte[] buff = ToolsUtil.hex2bin(strHex);
        return parse(buff);
    }

    /**
     * 信息解码
     *
     * @return
     */
    public CpcMfEf02Model decoded() {
        CpcMfEf02Model dd = new CpcMfEf02Model();
        dd.electricityInfo = (electricityInfo[0] & 0xFF) >> 7;
        dd.electricityRate = (electricityInfo[0] & 0x7F);
        dd.workState = (workState[0] & 0xFF) == 0 ? 0 : 1;

        return dd;
    }
}
